<template>
	<view>

		<!-- 添加记工弹窗 -->
		<u-popup v-model="addShow" mode="bottom" border-radius="20" :mask-close-able="false" closeable close-icon-size="40" duration="0"
			safe-area-inset-bottom @close="closeClick">
			<view class="keepEdit_box">
				<view class="handleList_box">
					<view class="setHead_con">
						<view class="setTit">添加记工</view>
					</view>
				</view>

				<!-- 间隔槽 -->
				<u-gap height="20" bg-color="#F5F5F5"></u-gap>

				<view class="handleList_box">
					<view class="handleList_con">
						<view class="handleLabel">类型：</view>
						<view class="handleList">
							<view :class="'handleItem '+(selectType==1?'handleActive':'')" @click="typeClick(1)">点工
							</view>
							<view :class="'handleItem '+(selectType==2?'handleActive':'')" @click="typeClick(2)">包工
							</view>
						</view>
					</view>
					<view class="handleList_con" v-if="selectType==1">
						<view class="handleLabel">上班：</view>
						<view class="handleList">
							<view :class="'handleItem '+(workType==1?'handleActive':'')" @click="workTypeSelect(1)">1工
							</view>
							<view :class="'handleItem '+(workType==2?'handleActive':'')" @click="workTypeSelect(2)">0.5工
							</view>
							<view :class="'handleItem '+(workType==3?'handleActive':'')" @click="workTypeSelect(3)">休息
							</view>
							<view :class="'handleItem '+(workType==4?'handleActive':'')" @click="workTypeSelect(4)">
								<input type="digit" :value="workInput" @input="workNumInput" placeholder="自定义">
							</view>
						</view>
					</view>
					<view class="handleList_con" v-if="selectType==1">
						<view class="handleLabel">加班：</view>
						<view class="handleList">
							<view :class="'handleItem '+(selectOver==1?'handleActive':'')" @click="openWorkPopup(1)">
								无加班
							</view>
							<view :class="'handleItem '+(selectOver==2?'handleActive':'')" style="flex: 2;"
								@click="openWorkPopup(2)">
								<view class="overInput">{{(selectOver==2 && overWork>0) ? overWork+'工' : '按工算'}}</view>
								<view class="overLine"></view>
								<image src="/static/keep/editBtn.png" mode="" class="overIcon"></image>
							</view>
							<view :class="'handleItem '+(selectOver==3?'handleActive':'')" style="flex: 2;"
								@click="openWorkPopup(3)">
								<view class="overInput">{{(selectOver==3 && overWork>0) ? overWork+'时' : '按小时'}}</view>
								<view class="overLine"></view>
								<image src="/static/keep/editBtn.png" mode="" class="overIcon"></image>
							</view>
						</view>
					</view>
					<view class="handleList_con" v-if="selectType==2">
						<view class="handleLabel">金额：</view>
						<view class="handleList">
							<view class="baoCon">
								<input type="decimal" :value="workInput" @input="workNumInput" placeholder="点击输入金额"
									placeholder-class="placeholderColor">
								<text>元/人</text>
							</view>
						</view>
					</view>
				</view>
				<view class="handlePost_con">
					<view class="completeBtn" @click="addSubmit">记 工</view>
				</view>
			</view>
		</u-popup>

		<!-- 选择工时组件 -->
		<WorkHour ref="workHour"></WorkHour>

	</view>
</template>

<script>
	import WorkHour from "@/components/keep/workHour.vue"
	import {
		gegerateDates,
		dateEqual,
		formatDate
	} from '@/util/generateDates.js';

	export default {
		components: {
			WorkHour,
		},
		props: {
			// 类型
			// billType: {
			// 	type: String,
			// 	default: '2'
			// },
		},
		data() {
			return {
				// 默认工地信息
				defaultWorksite: JSON.parse(uni.getStorageSync('defaultWorksite')),

				// 添加弹窗
				addShow: false,
				dayNum: formatDate(new Date(), 'yyyy-MM-dd'),

				// 记工
				selectType: 1, // 默认点工
				workType: 1, // 默认1个工
				workNum: -1, // 提交的工
				workInput: '', // 输入的工
				selectOver: 1, // 加班
				overWork: 0,
			};
		},
		created() {

		},
		methods: {
			// 清空数据
			clearData() {
				console.log('开始执行数据清空')
				var _this = this;
				_this.selectType = 1;
				_this.workType = 0;
				_this.workNum = -1;
				_this.workInput = '';
				_this.overWork = 0;
				_this.selectOver = 1;
			},

			// 选择记工类型
			typeClick(type) {
				this.selectType = type;
			},

			// 自定义工输入
			workNumInput(e) {
				this.workNum = e.detail.value;
				this.workInput = e.detail.value;
			},

			// 上班类型选择
			workTypeSelect(type) {
				this.workType = type;
				var workNum = 1;
				if (type == 1) {
					workNum = 1;
				}
				if (type == 2) {
					workNum = 0.5;
				}
				if (type == 3) {
					workNum = '休';
				}
				this.workNum = workNum;
			},

			// 选择日期
			openWorkPopup(type) {
				this.selectOver = type;
				if (type == 1) {
					this.overWork = 0;
					return
				}
				this.$refs.workHour.workShow = true;
				this.$refs.workHour.doingType = type;
			},

			// 关闭设定弹窗
			closeClick() {
				var _this = this;
				_this.addShow = false;
				uni.$emit('watermark', _this.orderNote);
				uni.navigateBack({
					delta: 1
				})
			},

			addSubmit() {
				var _this = this;
				console.log(_this.logIds)
				if (!_this.dayNum) {
					uni.showModal({
						content: '请选择日期',
						showCancel: false,
					})
					return;
				}
				// role:1工人 2带班 3老板
				_this.$u.throttle(_this.recordSubmit, 3000);

			},

			recordSubmit() {
				var _this = this;
				var workDetail = _this.workDetail;
				if (_this.selectType == 1) {
					// 点工
					var para = {
						id: '',
						num: _this.workNum,
						price: 0,
						remark: '',
						imgs: '',
					}
					para['overtime_hour'] = 0;
					para['overtime_days'] = 0;
				} else if (_this.selectType == 2) {
					// 包工
					var para = {
						id: '',
						num: 0,
						price: _this.workNum,
						remark: '',
						imgs: '',
					}
				}

				if (_this.selectOver == 2) {
					para['overtime_days'] = _this.overWork;
					para['overtime_hour'] = 0;
				}
				if (_this.selectOver == 3) {
					para['overtime_hour'] = _this.overWork;
					para['overtime_days'] = 0;
				}

				_this.$u.post('/api/project.work/add_do', para).then(res => {
					_this.addShow = false;
					uni.showModal({
						content: '记工成功',
						showCancel: false,
					})
					//数据对比
					_this.updateCompare();
					//清空历史数据
					_this.clearData();
					// 更新数据
					// _this.$parent.successDoing(1);
				}).catch(res => {
					console.log(res);
				})
			},
			
			// 弹出框关闭处理
			overWorkUpdate() {
				
			},

			// 更新对比数据
			updateCompare() {
				var _this = this;
				var workDetail = _this.workDetail;
				_this.$u.post('/api/project.compare/projectCompare', {
					project_id: _this.defaultWorksite.project_id,
					type: '1', //1记工、2记账
					add_date: workDetail.add_date,
					member_id: workDetail.member_id,
				}).then(res => {
					console.log(res)
					console.log("==更新对比数据==")
					_this.getCheckCompare()
				}).catch(res => {
					console.log(res);
				})
			},
			
			// 获取记工偏差
			getCheckCompare() {
				var _this = this;
				_this.$u.get('/api/project.compare/checkCompare', {
					project_id: _this.defaultWorksite.project_id,
					type: '1', //1记工、2记账
				}).then(res => {
					uni.hideLoading();
					console.log("==获取记工偏差==");
					console.log(res);
					var resData = res.data;
					if (resData > 0) {
						_this.isError = true;
					} else {
						_this.isError = false;
					}
				}).catch(res => {
					console.log(res);
				})
			},

		},
	}
</script>

<style scoped lang="scss">
	/* 新增记工弹窗 */
	.keepEdit_box {
		max-height: 90vh;
		overflow-y: scroll;
	}

	.setHead_con {
		padding: 30rpx;
		overflow: hidden;
	}

	.setTit {
		width: 100%;
		font-size: 32rpx;
		font-weight: bold;
		color: #000;
		text-align: center;
	}

	.handleList_box {
		padding: 15rpx 30rpx;
	}

	.handPost_con {
		padding: 30rpx;
		border-top: 1rpx solid #eee;
		overflow: hidden;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.delBtn {
		width: 240rpx;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 28rpx;
		color: #FF4241;
		border: 2rpx solid #ff4241;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.saveBtn {
		width: 430rpx;
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		font-size: 28rpx;
		color: #fff;
		background-color: #327FE8;
		border-radius: 10rpx;
		overflow: hidden;
	}
</style>
